﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using FrameConstructor;


namespace TetrisShape
{
    public class Field
    {
        MainMenu mm = new MainMenu();
        private bool flag = true;
        private List<Shape> ShapeStorage = new List<Shape>();
        public List<Shape> ReturnShapeStorage { get { return ShapeStorage; } }

        private InputHandler _userInputHandler;

        Render rd = new Render();
        Frame fr = new Frame();
        Block bl = new Block();
        Shape globalShape = new Shape();

        public Field(InputHandler inputHandler)
        {
            _userInputHandler = inputHandler;
        }

        public Frame SetGameField()
        {
            if (ShapeStorage.Count == 0)
            {
                Console.Clear();
            }
            Console.ForegroundColor = ConsoleColor.White;
            fr.SetFrame(20, 20, 31, 41);
            return fr;
        }

        public void Move()
        {
            Shape sh = new Shape();
            bl.ChangeSpeed();
            globalShape = sh;
            sh.CreateRandomShape(SetGameField(), sh);
            int i = 0;
            do
            {
                _userInputHandler.QueryKeyBoard();
                //rd.ClearShape(sh, fr);
                //rd.PrintShape(sh);
                rd.Print(sh, fr, MainMenu.globalF);
                bl.DoStep(sh);
                i++;
            } while (IsPositionFree(sh));

            ShapeStorage.Add(sh);
        }

        public void StartMoveShape()
        {
            _userInputHandler.MoveLeft += _userInputHandler_GoMoveLeft;
            _userInputHandler.MoveRight += _userInputHandler_GoMoveRight;
            _userInputHandler.SpeedUp += _userInputHandler_GoSpeedUp;
            _userInputHandler.Rotate += _userInputHandler_GoRotate;

            do
            {
                Move();
            } while (flag);
        }

        void _userInputHandler_GoMoveRight(object sender, EventArgs e)
        {
            bl.BlockMoveRight(globalShape, fr);
        }

        void _userInputHandler_GoMoveLeft(object sender, EventArgs e)
        {
            bl.BlockMoveLeft(globalShape, fr);
        }

        void _userInputHandler_GoSpeedUp(object sender, EventArgs e)
        {
            Block.Speed = 0;
        }

        void _userInputHandler_GoRotate(object sender, EventArgs e)
        {
            if (globalShape.ShapeType == "Line")
            {
            globalShape.RotateLine();
            }
        }

        public bool IsPositionFree(Shape sh)
        {
            if (ShapeStorage.Count != 0)
            {
                foreach (Shape currentShape in sh.ReturnShape)
                {
                    foreach (Block currentBlock in sh.ReturnBlock)
                    {
                        foreach (Shape shapeInStorage in ShapeStorage)
                        {
                            foreach (Block blockInStorage in shapeInStorage.ReturnBlock)
                            {
                                if ((currentBlock.X == blockInStorage.X && currentBlock.Y == blockInStorage.Y - 1) || currentBlock.Y == fr.StartY + 20)
                                {
                                    if (currentBlock.Y <= fr.StartY + 5)
                                    {
                                        foreach (Block curBlock in sh.ReturnBlock)
                                            if (curBlock.Y == fr.StartY + 2)
                                            {
                                                flag = false;
                                                return false;
                                            }
                                    }
                                    return false;
                                }
                            }
                        }
                    }
                }
            }
            else
            {
                foreach (Shape shape in sh.ReturnShape)
                {
                    foreach (Block block in sh.ReturnBlock)
                    {
                        if (block.Y == fr.StartY + 20)
                            return false;
                    }
                }
            }
            return true;
        }

    }
}
